/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionemu.gameserver.services;

import com.aionemu.gameserver.model.ChatType;
import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.NpcWhichNotGatherable;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.RequestResponseHandler;
import com.aionemu.gameserver.model.templates.npcTemplate.BuyListData;
import com.aionemu.gameserver.model.templates.npcTemplate.NpcTemplate;
import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
import com.aionemu.gameserver.network.aion.serverpackets.SM_MESSAGE;
import com.aionemu.gameserver.network.aion.serverpackets.SM_PET;
import com.aionemu.gameserver.network.aion.serverpackets.SM_PLASTIC_SURGERY;
import com.aionemu.gameserver.network.aion.serverpackets.SM_QUESTION_WINDOW;
import com.aionemu.gameserver.network.aion.serverpackets.SM_REPURCHASE;
import com.aionemu.gameserver.network.aion.serverpackets.SM_SELL_ITEM;
import com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionemu.gameserver.network.aion.serverpackets.SM_TRADELIST;
import com.aionemu.gameserver.questEngine.QuestEngine;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.restrictions.RestrictionsManager;
import com.aionemu.gameserver.utils.MathUtil;
import com.aionemu.gameserver.utils.PacketSendUtility;

/**
 * @author Mr. Poke
 *
 */
public class DialogService
{
	
	public static void onDialogSelect(int dialogId, final Player player, NpcWhichNotGatherable object, int questId)
	{
		if (!MathUtil.isInRange(object, player, 10))
			return;
		int targetObjectId = object.getObjectId();

		if (QuestEngine.getInstance().onDialog(new QuestEnv(object, player, questId, dialogId)))
			return;
		switch (dialogId)
		{
			case 2:
				BuyListData tradeListData;
				try
				{
					tradeListData = ((NpcTemplate)object.getObjectTemplate()).getFunctionData().getBuyListData();
				}
				catch (NullPointerException e)
				{
					PacketSendUtility.sendMessage(player, "Buy list is missing!!");
					return;
				}
				if (tradeListData == null)
				{
					PacketSendUtility.sendMessage(player, "Buy list is missing!!");
					break;
				}
				PacketSendUtility.sendPacket(player, new SM_TRADELIST(object, tradeListData));
				break;
			case 3:
				PacketSendUtility.sendPacket(player, new SM_SELL_ITEM(targetObjectId, player.getPrices().getVendorSellModifier()));
				break;
			case 4:
				// stigma
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 1));
				break;
			case 5:
				// create legion
				if (MathUtil.isInRange(object, player, 10)) // avoiding exploit with sending fake dialog_select packet
				{
					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 2));
				}
				else
				{
					PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.LEGION_CREATE_TOO_FAR_FROM_NPC());
				}
				break;
			case 6:
				// disband legion
				if (MathUtil.isInRange(object, player, 10)) // avoiding exploit with sending fake dialog_select packet
				{
					player.getLegion().requestDisbandLegion(object, player);
				}
				else
				{
					PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.LEGION_DISPERSE_TOO_FAR_FROM_NPC());
				}
				break;
			case 7:
				// recreate legion
				if (MathUtil.isInRange(object, player, 10)) // voiding exploit with sending fake client dialog_select
				// packet
				{
					player.getLegion().recreateLegion(object, player);
				}
				else
				{
					PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.LEGION_DISPERSE_TOO_FAR_FROM_NPC());
				}
				break;
			case 20:
				// warehouse
				if (MathUtil.isInRange(object, player, 10)) // voiding exploit with sending fake client dialog_select
				// packet
				{
					if (!RestrictionsManager.canUseWarehouse(player))
						return;

					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 26));
					WarehouseService.sendWarehouseInfo(player, true);
				}
				break;
			case 27:
				// Consign trade?? npc karinerk, koorunerk
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 13));
				break;
			case 29:
				// soul healing
				final long expLost = player.getObjectTemplate().getExpRecoverable();
				final double factor = (expLost < 1000000 ? 0.25 - (0.00000015 * expLost) : 0.1);
				final int price = (int) (expLost * factor);

				RequestResponseHandler responseHandler = new RequestResponseHandler(object)
				{
					@Override
					public void acceptRequest(VisibleObject requester, Player responder)
					{
						if (Item.decreaseKinah(player, price))
						{
							PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.EXP(String.valueOf(expLost)));
							PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.SOUL_HEALED());
							player.getObjectTemplate().resetRecoverableExp();
						}
						else
						{
							PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.NOT_ENOUGH_KINAH(price));
						}
					}

					@Override
					public void denyRequest(VisibleObject requester, Player responder)
					{
						// no message
					}
				};
				if (player.getObjectTemplate().getExpRecoverable() > 0)
				{
					boolean result = player.getResponseRequester().putRequest(SM_QUESTION_WINDOW.STR_SOUL_HEALING, responseHandler);
					if (result)
					{
						PacketSendUtility.sendPacket(player, new SM_QUESTION_WINDOW(SM_QUESTION_WINDOW.STR_SOUL_HEALING, 0, String.valueOf(price)));
					}
				}
				else
				{
					PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.DONT_HAVE_RECOVERED_EXP());
				}
				break;
			case 30:
				switch (object.getNpcId())
				{
					case 204089:
						TeleportService.teleportTo(player, 120010000, 1, 984f, 1543f, 222.1f, 0);
						break;
					case 203764:
						TeleportService.teleportTo(player, 110010000, 1, 1462.5f, 1326.1f, 564.1f, 0);
						break;
					case 203981:
						TeleportService.teleportTo(player, 210020000, 1, 439.3f, 422.2f, 274.3f, 0);
						break;
				}
				break;
			case 31:
				switch (object.getNpcId())
				{
					case 204087:
						TeleportService.teleportTo(player, 120010000, 1, 1005.1f, 1528.9f, 222.1f, 0);
						break;
					case 203875:
						TeleportService.teleportTo(player, 110010000, 1, 1470.3f, 1343.5f, 563.7f, 21);
						break;
					case 203982:
						TeleportService.teleportTo(player, 210020000, 1, 446.2f, 431.1f, 274.5f, 0);
						break;
				}
				break;
			case 35:
				// Godstone socketing
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 21));
				break;
			case 36:
				// remove mana stone
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 20));
				break;
			case 37:
				// modify appearance
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 19));
				break;
			case 38:
				TeleportService.showMap(object, player);
				break;
			case 39:
				// improve extraction
			case 40:
				// learn tailoring armor smithing etc...
				CraftSkillUpdateService.getInstance().learnSkill(player, object);
				break;
			case 41:
				// expand cube
				CubeExpandService.expandCube(player, object);
				break;
			case 42:
				WarehouseService.expandWarehouse(player, object);
				break;
			case 47:
				// legion warehouse
				if (MathUtil.isInRange(object, player, 10) && player.isInLegion())
				{
					player.getLegion().openLegionWarehouse(player, object.getObjectId());
				}
				break;
			case 50:
				// WTF??? Quest dialog packet
				break;
			case 52:
				if (MathUtil.isInRange(object, player, 10)) // avoiding exploit with sending fake dialog_select packet
				{
					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 28));
				}
				break;
			case 53:
				// coin reward
				PacketSendUtility.sendPacket(player, new SM_MESSAGE(0, null, "This feature is not available yet", ChatType.ANNOUNCEMENTS));
				break;
			case 55:
				/** @author IlBuono */
				if (player.getInventory().getItemCountByItemId(169650000) > 0 || player.getInventory().getItemCountByItemId(169650001) > 0)
					PacketSendUtility.sendPacket(player, new SM_PLASTIC_SURGERY(player, true, false));
				else
					PacketSendUtility.sendPacket(player, new SM_PLASTIC_SURGERY(player, false, false));
				player.setEditMode(true);
				break;
			case 56:
				/** @author IlBuono */
				if (player.getInventory().getItemCountByItemId(169660000) > 0 || player.getInventory().getItemCountByItemId(169660001) > 0)
					PacketSendUtility.sendPacket(player, new SM_PLASTIC_SURGERY(player, true, true));
				else
					PacketSendUtility.sendPacket(player, new SM_PLASTIC_SURGERY(player, false, true));
				player.setEditMode(true);
				break;
			case 60:
				// armsfusion
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 29));
				break;
			case 61:
				// armsbreaking
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, 30));
				break;
			case 64:
				//Repurchase
				PacketSendUtility.sendPacket(player, new SM_REPURCHASE(player.getRepurchaseItems(), targetObjectId));
				break;
			case 65:
				// adopt pet
				PacketSendUtility.sendPacket(player, new SM_PET(6));
				break;
			case 66:
				// surrender pet
				PacketSendUtility.sendPacket(player, new SM_PET(7));
				break;
			default:
				if (questId > 0)
					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, dialogId, questId));
				else
					PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(targetObjectId, dialogId));
				break;
		}
	}
}
